<?php

namespace post\modules\user\controllers;

use post\modules\user\models\User;
use post\modules\user\models\UserInfo;
use Yii;

use yii\db\Exception;
use yii\web\Controller;
use post\base\BaseController;

class RegisterController extends BaseController {
	
	
	public function actionIndex() {

	    $request=Yii::$app->request;
	    if($request->isPost)
        {
            $arr=['user_name','user_email','user_pwd'];
            $data_info= $this->data_validate($arr);

            if($data_info=="false")
            {
                return $this->json_error("参数不全");
            }

            if(User::find()->where(['email'=>$data_info['user_email']])->one()!=null){
                return $this->json_error("邮箱已经被注册!");
            }


            $salt = '$2y$11$' . substr(md5(uniqid(rand(), true)), 0, 22);


            $password=crypt($data_info['user_pwd'], $salt);

            $tr=Yii::$app->db->beginTransaction();

            try{
                $user=new User();

                $user->username=$data_info['user_name'];
                $user->email=$data_info['user_email'];
                $user->password=$password;
                $user->salt=$salt;
                $token=md5(uniqid().time().$salt);
                $user->access_token=$token;
                $info= $user->save();
                $id = $user->attributes['id'];

                $userInfo=new UserInfo();
                $userInfo->user_id=$id;
                $userInfo->tou="http://p7h441d6x.bkt.clouddn.com/2018-04-25_5ae035d41f9a2.jpg";
                $userInfo->save();
                $tr->commit();

            }catch (Exception $exception)
            {
                $tr->rollBack();
                return $this->json_error("注册失败!");

            }


            return  $this->json_ok($info);
        }


		
	}

	
	
}
